package com.llf.Blog.Controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Blog.Mapper.TagMapper;
import com.llf.Blog.Pojo.Blog;
import com.llf.Blog.Pojo.BlogTags;
import com.llf.Blog.Pojo.Tag;
import com.llf.Blog.Pojo.Type;
import com.llf.Blog.Service.BlogService;
import com.llf.Blog.Service.BlogTagsService;
import com.llf.Blog.Service.TagService;
import com.llf.Blog.ServiceImpl.BlogServiceImpl;
import com.llf.Blog.ServiceImpl.BlogTagsServiceImpl;
import com.llf.Blog.ServiceImpl.TagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author machenike
 */
@Controller
@Api(value = "提供获取标签信息接口",tags = "博客标签管理")
public class TagsController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagsService blogTagsService;


    /**
     * 通过点击Tags类型完成搜索并返回结果,首次进入默认TagsID为1
     * @param model
     * @param pagNum
     * @param id tagID
     * @return
     */
    @ApiOperation("根据博客标签搜索接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pagNum", value = "当前页",required = true),
            @ApiImplicitParam(dataType = "long",name = "id", value = "标签id",required = true)
    })
    @GetMapping("/toTags/{id}")
    public String toTagsByTagId(Model model,  Integer pagNum,@PathVariable("id") Long id){
        if (pagNum==null){
            pagNum=1;
        }
        PageHelper.startPage(pagNum,4);
        List<Blog> blogList = blogService.getBlogByTagId(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        PageHelper.clearPage();
        long n=id;
        List<BlogTags> tagList = blogTagsService.getCountTag();
        model.addAttribute("tagList",tagList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("n",id);
        return "tags";
    }


    /**
     * 进入后台标签页 初次进入默认为第一页
     * @param model
     * @param current 当前页
     * @param pageSize 每页大小
     * @return
     */
    @ApiOperation("后台标签页跳转接口")
    @GetMapping("/admin/toTags/{current}")
    public String toAdminTypes(Model model, @PathVariable("current") Integer current, Integer pageSize){
        BaseMapper<Tag> mapper = tagService.getBaseMapper();
        pageSize=4;
        Page<Tag> page = new Page<Tag>(current, 4);
        Page<Tag> tagPage = mapper.selectPage(page, null);
        model.addAttribute("page",tagPage);
        return "admin/tags";
    }


    /**
     * 进去标签添加页
     * @return
     */
    @ApiOperation("后台标签添加页跳转接口")
    @GetMapping("/admin/toTagAdd")
    public String toTagAdd(){
         return "admin/tag-add";
    }


    /**
     * 添加一个新的标签
     * @param tag 新的标签对象
     * @param attributes 传递消息
     * @param name 通过名字查询 确定标签是否存在
     * @param model
     * @return
     */
    @ApiOperation("标签添加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "name", value = "新增标签名字",required = true)
    })
    @PostMapping("/insertTag")
    public String insertType(@RequestBody Tag tag, RedirectAttributes attributes, String name, Model model){
        Tag tag1 = tagService.getByName(name);
            if (tag1 != null) {
                model.addAttribute("error","该标签已存在");
                return "admin/tag-add";
            }else {
                 tagService.insertTag(tag);
             //使用它 可以给前端传递一个提示框
                attributes.addFlashAttribute("message","添加成功");
                return "redirect:/admin/toTags/1";
            }
    }


    /**
     *  根据TagId删除一个标签
     * @param id TagId
     * @param attributes 传递消息
     * @return
     */
    @ApiOperation("标签删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long",name = "id", value = "标签id",required = true)
    })
    @GetMapping("/deleteTag/{id}")
    public String deleteByTag( @PathVariable("id") long id,RedirectAttributes attributes){
        tagService.deleteById(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/toTags/1";
    }


    /**
     *  跳转标签更新页
     * @param id 根据TagId 查询出这个标签对象
     * @param model 携带标签对象跳转
     * @return
     */
    @ApiOperation("后台标签更新页跳转接口")
    @GetMapping("/toUpdateTag/{id}")
    public String toUpdateTag( @PathVariable("id") long id,Model model){
        Tag tag = tagService.getById(id);
        model.addAttribute("tag",tag);
        return "admin/tag-update";
    }


    /**
     * 更新标签
     * @param tag 接受传递的标签对象 并在前端默认显示
     * @param model
     * @param attributes 消息传递
     * @param oldName 更改前的名字
     * @return
     */
    @ApiOperation("标签更新接口")
    @ApiImplicitParams({

            @ApiImplicitParam(dataType = "string",name = "oldName", value = "更改之前的名字",required = true)
    })
    @PostMapping("/updateByTag")
    public String updateByTag(@RequestBody Tag tag,Model model,RedirectAttributes attributes,String oldName){
        if (tagService.getByName(tag.getName())!=null){
                 model.addAttribute("error","该分类已存在");
                 return "admin/tag-update";
         }else {
                tagService.update(tag);
                attributes.addFlashAttribute("message","名称===>"+oldName+"<===修改成功为====>"+tag.getName());
                return "redirect:/admin/toTags/1";
        }
    }

}

